package com.teamcalendar.client;

import com.teamcalendar.client.controller.R;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageCursorAdapter extends SimpleCursorAdapter {

        private Cursor c;
        private Context context;

	public ImageCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		this.c = c;
		this.context = context;
	}

	public View getView(int pos, View inView, ViewGroup parent) {
	       View v = inView;
	       if (v == null) {
	            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = inflater.inflate(R.layout.member_row, null);
	       }
	       this.c.moveToPosition(pos);		
	       String firstName = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_FIRSTNAME));
	       String lastName = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_LASTNAME));
	       String phoneNumber = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_PHONENUMBER));
	       String email = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_EMAIL));

	       byte[] favicon = this.c.getBlob(this.c.getColumnIndex(DBAdapter.KEY_MEMBER_IMAGE));
	       if (favicon != null) {
		   ImageView iv = (ImageView) v.findViewById(R.id.member_imageView);
	           iv.setImageBitmap(BitmapFactory.decodeByteArray(favicon, 0, favicon.length));
	       }
               TextView firstNameView = (TextView) v.findViewById(R.id.member_firstname);
               TextView lastNameView = (TextView) v.findViewById(R.id.member_lastname);
               TextView phoneNumberView = (TextView) v.findViewById(R.id.member_phonenumber);
               TextView emailView = (TextView) v.findViewById(R.id.member_email);

               firstNameView.setText(firstName);
               lastNameView.setText(lastName);
               phoneNumberView.setText(phoneNumber);
               emailView.setText(email);

	       return(v);
	}

}


